package com.a0soft.gphone.aTruffleHog.wnd;
/*
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 3, or (at your option) any
 * later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * Copyright(C) 2011 
 *   Sam Lu <ysl.sam.lu (at) gmail.com>
 *
 */

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.KeyEvent;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.a0soft.gphone.aTruffleHog.R;
import com.a0soft.gphone.aTruffleHog.gio.ui.BaseActivity;

public class AboutWnd extends BaseActivity {
    private WebView m_web;
    
    //private static final String LOG_TAG = AboutWnd.class.getSimpleName();

    ////////////////////////////////////////////
    @Override public void 
    onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.about_wnd);
        getActivityHelper().setupActionBar(getTitle(), 0);
        
        _SetupViews(getIntent().getExtras());
    }
    
    @Override protected void 
    onPostCreate(Bundle bdSavedInstanceState) 
    {
        super.onPostCreate(bdSavedInstanceState);
        
        getActivityHelper().setupSubActivity();
    }
    
    @Override public boolean 
    dispatchKeyEvent(KeyEvent event)
    {
        if ((event.getKeyCode() == KeyEvent.KEYCODE_BACK) &&
             (event.getAction() == KeyEvent.ACTION_DOWN)) {
            _Backward();
            return true;
        }
        return super.dispatchKeyEvent(event);
    }
    
    private void
    _Backward()
    {
        if (m_web.canGoBack())
            m_web.goBack();
        else
            finish();       
    }
    
    public static String
    GetAssertRootPath(Context ctx)
    {
        final String strLang = ctx.getResources().getConfiguration().locale.getLanguage();

        String strLangURL = "file:///android_asset/";
        
        if (strLang.equalsIgnoreCase("zh")) strLangURL += "zh/";
        
        return strLangURL;
    }
    
    private void 
    _SetupViews(Bundle bd)
    {
        m_web = (WebView)findViewById(R.id.web);
        m_web.setWebViewClient(new MyWebViewClient());
        
        String strURL = GetAssertRootPath(this);
        
        strURL += "about.htm";
        
        m_web.loadUrl(strURL);
    }
    
    final class MyWebViewClient extends WebViewClient {
        //fixed the dead link problem for Samsung devices
        @Override public boolean
        shouldOverrideUrlLoading(WebView view, String url) 
        {
            try {
                if (url.startsWith("http:") || url.startsWith("mailto:") || 
                    url.startsWith("https:")) {
                    Intent it = new Intent(Intent.ACTION_VIEW);
                    it.setData(Uri.parse(url));
                    it.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    startActivity(it);
                    return true;
                }
            } catch (Exception e) {
            }
            
            view.loadUrl(url);
            return true;
        }
    }
}
